/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-4-10
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.core.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.pearlsoft.webapp.core.dao.dialect.DialectFactory;
import com.pearlsoft.webapp.core.dataset.Record;

/**
 * 描述：
 * 所有的Dao的基类
 * 
 * 
 */
@Repository
public  class BaseSimpleJdbcDao extends SimpleJdbcDaoSupport implements BaseDao {
	private DialectFactory dialectFactory;
	@Autowired
	private void setDialectFactory(DialectFactory dialectFactory){
		this.dialectFactory = dialectFactory;
	}

	public int queryForInt(final String sql, final Map paramMap) {
		return this.getSimpleJdbcTemplate().queryForInt(sql, paramMap);
	}

	public int queryForInt(final String sql) {
		return this.getSimpleJdbcTemplate().queryForInt(sql);
	}

	public int queryForInt(final String sql, final Object... params) {
		return this.getSimpleJdbcTemplate().queryForInt(sql, params);
	}

	public long queryForLong(final String sql, final Map paramMap) {
		return this.getSimpleJdbcTemplate().queryForLong(sql,paramMap);
	}

	public long queryForLong(final String sql) {
		return this.getSimpleJdbcTemplate().queryForLong(sql);
	}

	public long queryForLong(final String sql, final Object... params) {
		return this.getSimpleJdbcTemplate().queryForLong(sql, params);
	}

	public List<Record> query(final String sql, final Map<String,?> paramMap,
			final RecrodRowMapper<Record> rowMapper) {
		return this.getSimpleJdbcTemplate().query(sql, rowMapper, paramMap);
	}
	
	public List<Record> query(final String sql, 
			final RecrodRowMapper<Record> rowMapper) {
		return this.getSimpleJdbcTemplate().query(sql,rowMapper);
	}

	public List<Record>  query(final String sql, 
			final  RecrodRowMapper<Record> rowMapper,final Object...params) {
		return this.getSimpleJdbcTemplate().query(sql, rowMapper, params);
	}
	
	public Record queryOne(final String sql, final Map<String,?> paramMap,
			final RecrodRowMapper<Record> rowMapper) {
			List<Record> list= this.getSimpleJdbcTemplate().query(sql, rowMapper, paramMap);
		if( list!=null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public Record queryOne(final String sql, 
			final RecrodRowMapper<Record> rowMapper) {
		List<Record> list = this.getSimpleJdbcTemplate().query(sql,rowMapper);
		if(list!=null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public Record  queryOne(final String sql, 
			final  RecrodRowMapper<Record> rowMapper,final Object...params) {
		List<Record> list =  this.getSimpleJdbcTemplate().query(sql, rowMapper, params);
		if(list!=null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}		
	}	
	
	
	public PageResult findByPage( final String sql,
			final int offset, final int pageSize, final Map<String,?> paramMap,
			final RecrodRowMapper<Record> rowMapper) {
		PageResult pageResult = new PageResult();
		String countSql=" select count(*) from ( "+sql+" ) as _tempView";
		int total = this.queryForInt(countSql, paramMap);
		String pageSql = this.dialectFactory.getDialect().getLimitSqlString(sql, offset, pageSize) ;
		List<Record> records = null;
		if( total > 0 ) {
			records = this.query(pageSql, paramMap, rowMapper);
		}
		pageResult.setRecords(records);
		pageResult.setTotalCount(total);
		return pageResult;
	}

	public PageResult findByPage( final String sql,
			final int offset, final int pageSize,
			final  RecrodRowMapper<Record> rowMapper, Object...params) {
	
		PageResult pageResult = new PageResult();
		String countSql=" select count(*) from ( "+sql+" ) as _tempView";
		int total = this.queryForInt(countSql, params);
		
		List<Record> records = null;
		String pageSql = this.dialectFactory.getDialect().getLimitSqlString(sql, offset, pageSize) ;
		if( total > 0 ) {			
			records = this.query(pageSql, rowMapper, params);
		}
		pageResult.setRecords(records);
		pageResult.setTotalCount(total);
		return pageResult;	
	
	
	}
	
	public int update(final String sql, final Map<String,?> paramMap) {
		return this.getSimpleJdbcTemplate().update(sql, paramMap);
	}
	
	public int update(final String sql, final Object...params) {
		return this.getSimpleJdbcTemplate().update(sql, params);
	}
	
	public int[] batchUpdate(final String sql, final Map<String,?>[] paramMaps) {
		return this.getSimpleJdbcTemplate().batchUpdate(sql, paramMaps);
	}
	
}
